package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.MyWalletDrawLogsAdapter;
import com.eboss.sbs.config.Config;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.WalletDrawLogEntity;
import com.eboss.sbs.model.WalletDrawLogs;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyWalletDrawLogActivity extends BaseActivity {

	private PullToRefreshScrollView scroolView;
	
	private MyListView listview;
	
	private MyWalletDrawLogsAdapter adapter;
	
	private List<WalletDrawLogEntity> datas = new ArrayList<WalletDrawLogEntity>();
	
	private int page = 1;
	private int rows = Config.ROWS;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_my_wallet_draw_log);
		super.onCreate(savedInstanceState);
		initView();
		getdata();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "提现记录");
		
		scroolView = (PullToRefreshScrollView) findViewById(R.id.scroolView);
		scroolView.setOnRefreshListener(new MyOnRefreshListener());
		scroolView.setMode(Mode.BOTH);
		
		listview = (MyListView) findViewById(R.id.listview);
		adapter = new MyWalletDrawLogsAdapter(mActivity, datas);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new ListItemClickEvent());
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
	}
	
	//下拉刷新 上拉加载
	class MyOnRefreshListener implements OnRefreshListener2<ScrollView> {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			scroolView.setMode(Mode.BOTH);
			String label = DateUtils.formatDateTime(mActivity, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
			refreshData();
		}
		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			page++;
			getdata();
		}
	}
	
	class ListItemClickEvent implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			Intent intent = new Intent(mActivity, MyWalletDrawLogDetailActivity.class);
			intent.putExtra("logid", datas.get(arg2).getLogid());
			startActivity(intent);
		}
	}
	
	private void refreshData(){
		datas.clear();
		page = 1;
		getdata();
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("pagenum", page);
		params.put("pagesize", rows);
		API.http_post(this, API.WALLET_DRAW_LOG, params, new HttpCallback<WalletDrawLogs>(this) {
			@Override
			public void onSuccess(int statusCode, WalletDrawLogs data) {
				if(data!=null){
					 List<WalletDrawLogEntity> tmp_datas = data.getDrawLogs();
					 if(tmp_datas==null||tmp_datas.size()==0||tmp_datas.size()<rows){
						if(tmp_datas==null||tmp_datas.size()==0){
							toast("没有更多数据");
						}
						scroolView.setMode(Mode.PULL_DOWN_TO_REFRESH);
					 }
					 if(tmp_datas!=null&&tmp_datas.size()>0){
						 datas.addAll(tmp_datas);
					 }
				}
				boolean no_datas = (datas==null||datas.size()==0);
				//showView(R.id.eva_nodata, no_datas);
				showView(R.id.listview, !no_datas);
				adapter.notifyDataSetChanged();
				scroolView.onRefreshComplete();
			}

			@Override
			public void onFailure(int statusCode, String message) {
				//-------------- this part below is test content ---------------//
				List<WalletDrawLogEntity> logs = new ArrayList<WalletDrawLogEntity>();
				logs.add(new WalletDrawLogEntity(1, Long.parseLong("1479804790743"), 88888, 5000, 1));
				logs.add(new WalletDrawLogEntity(2, Long.parseLong("1479804690743"), 78888, 4000, 2));
				logs.add(new WalletDrawLogEntity(3, Long.parseLong("1479804590743"), 68888, 5000, 3));
				logs.add(new WalletDrawLogEntity(4, Long.parseLong("1479804490743"), 58888, 6000, 1));
				WalletDrawLogs data_tmp = new WalletDrawLogs(123456, 2345, 99999, logs);
				setTvText(R.id.tv_successDrawed, StringUtil.getMoneyString(data_tmp.getSuccessDrawed())+" 元");
				setTvText(R.id.tv_unsettled, StringUtil.getMoneyString(data_tmp.getUnsettled())+" 元");
				setTvText(R.id.tv_balance, StringUtil.getMoneyString(data_tmp.getBalance())+" 元");
				datas.addAll(logs);
				adapter.notifyDataSetChanged();
				scroolView.onRefreshComplete();
				//-------------- this part above is test content ---------------//
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<WalletDrawLogs>() {};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			default:break;
		}
		super.onClick(v);
	}
	
}
